﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using UI.BudgetManage;
using BL.Client.BudgetManageBL;
using Common.ClassLib;
using Common.IO;
using Common.ExpressionAnalysis;
using Common.UIFunction;

namespace UI.BudgetManage.ChildForm.LocalBudgetReport
{
    public partial class InputBudgetReport : Form
    {
        public InputBudgetReport()
        {
            
        }

        public InputBudgetReport(string report_user_id)
        {
            InitializeComponent();
            this.ReportUserId = report_user_id;
        }

        string ReportUserId;
        BudgetManageServerice BMS;

        private void InputBudgetReport_Load(object sender, EventArgs e)
        {
            this.BMS = ((BMMain)this.MdiParent).BMS;

            InputTable table = new InputTable(this.dgvInput);
            int[] width = { 350, 350 };
            string[] context = { "预算范畴：", "预算名称", "上报部门：", "上报科室：", 
                "上报人：", "上报预算一级科目：", "上报预算二级科目：" , 
                "开支方式：","采购方式：", "上报金额(元)","上报备注："};
            Control[] controls = { cbBudgetRange, tbBudgetName, tbDeptPName, tbDeptName, 
                tbUserRealName, cbBudgetMainType, cbBudgetSubType, 
                cbPayMethod, cbPurchaseMethod,tbBudgetPrice,   tbBudgetRemark };
            table.InputTableBound(context, controls, width);


            tbDeptPName.Text = BMS.GetDeptPNameByUserId(this.ReportUserId);
            tbDeptName.Text = BMS.GetDeptNameByUserId(this.ReportUserId);
            tbUserRealName.Text = BMS.GetUserRealName(this.ReportUserId);
            
            ////////Init cbBudgetMainType///////////
            cbBudgetMainType.Items.Clear();
            cbBudgetMainType.Text = "请选择";
            DataSet ds = this.BMS.GetBudgetType();
            if (ds == null)
            {
                MessageBox.Show("请检查网络连接！");
                return;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    cbBudgetMainType.Items.Add(
                        new ComboBoxItem(dr["budget_type_name"].ToString(),
                        dr["budget_type_id"].ToString()));
                }
            }

            ////////Init cbPayMethod///////////

            cbPayMethod.Items.Clear();
            cbPayMethod.Text = "请选择";
            ds = this.BMS.GetPayMethod();
            if (ds == null)
            {
                MessageBox.Show("请检查网络连接！");
                return;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    cbPayMethod.Items.Add(
                        new ComboBoxItem(dr["pay_method_name"].ToString(),
                        dr["pay_method_id"].ToString()));
                }
                if (cbPayMethod.Items.Count > 0)
                {
                    cbPayMethod.SelectedIndex = 0;
                }
            }

            cbBudgetRange.Items.Clear();
            cbBudgetRange.Text = "请选择";
            ds = this.BMS.GetBudgetRange();
            if (ds == null)
            {
                MessageBox.Show("请检查网络连接！");
                return;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    cbBudgetRange.Items.Add(
                        new ComboBoxItem(dr["budget_range_name"].ToString(),
                        dr["budget_range_id"].ToString()));
                }
                if (cbBudgetRange.Items.Count > 0)
                {
                    cbBudgetRange.SelectedIndex = 0;
                }
            }

            cbPurchaseMethod.Items.Clear();
            cbPurchaseMethod.Text = "请选择";
            ds = this.BMS.GetPurchaseMethod();
            if (ds == null)
            {
                MessageBox.Show("请检查网络连接！");
                return;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    cbPurchaseMethod.Items.Add(
                        new ComboBoxItem(dr["purchase_method_name"].ToString(),
                        dr["purchase_method_id"].ToString()));
                }
                if (cbPurchaseMethod.Items.Count > 0)
                {
                    cbPurchaseMethod.SelectedIndex = 0;
                }
            }
        }

        private void BtnReport_Click(object sender, EventArgs e)
        {
            string budget_range_id = ((ComboBoxItem)(cbBudgetRange.SelectedItem)).Value;
            string budget_name = tbBudgetName.Text;
            string budget_price = tbBudgetPrice.Text.Trim();
            string pay_method_id = ((ComboBoxItem)(cbPayMethod.SelectedItem)).Value;
            string purchase_method_id = ((ComboBoxItem)(cbPurchaseMethod.SelectedItem)).Value;
            string budget_remark = tbBudgetRemark.Text;
            string budget_type_id;
            string[,] budget_item_info = new string[dgvBudgetItem.Rows.Count - 1, 4];
            if (budget_range_id == "" || budget_price == "0" || budget_name == "" ||
                cbBudgetMainType.SelectedItem == null || cbBudgetSubType.SelectedItem ==null )
            {
                MessageBox.Show("请完整填写信息！");
                return;
            }
            else
            {
                budget_type_id = ((ComboBoxItem)cbBudgetSubType.SelectedItem).Value;
                for(int i = 0; i < dgvBudgetItem.Rows.Count -1; i++)
                {
                    if (dgvBudgetItem.Rows[i].Cells["BudgetItemUnit"].Value == null ||
                        dgvBudgetItem.Rows[i].Cells["BudgetItemUnit"].Value.ToString() == "")
                        dgvBudgetItem.Rows[i].Cells["BudgetItemUnit"].Value = "个";
                    for (int j = 0; j < dgvBudgetItem.Rows[i].Cells.Count - 1; j++)
                    {
                        string value = dgvBudgetItem.Rows[i].Cells[j].Value.ToString().Trim();
                        if (value == null || value == "")
                        {
                            MessageBox.Show("请完整填写物品明细信息！");
                            return;
                        }
                        else if ((dgvBudgetItem.Columns[j].Name == "BudgetItemPrice" ||
                            dgvBudgetItem.Columns[j].Name == "BudgetItemCount") &&
                            !StringAnalysis.IsNumeric(value))
                        {
                            MessageBox.Show("请正确填写物品明细信息！");
                            return;
                        }
                        else
                        {
                            budget_item_info[i, j] = value;
                        }
                    }
                }


                bool bl = this.BMS.IBR.BudgetReport(budget_range_id, budget_name,
                            budget_price, pay_method_id, purchase_method_id, 
                            budget_type_id, budget_remark, this.ReportUserId, budget_item_info);
                if (bl)
                {
                    MessageBox.Show("上报成功！");
                    this.Reset();
                }

                else
                    MessageBox.Show("上报失败！");
            }
        }

        private void Reset()
        {
            this.dgvBudgetItem.Rows.Clear();
            this.tbBudgetName.Clear();
            this.tbBudgetRemark.Clear();
            this.tbBudgetPrice.Clear();
        }


        private void cbBudgetMainType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbBudgetSubType.Items.Clear();
            cbBudgetSubType.Text = "请选择";
            string budget_type_parentid = ((ComboBoxItem)cbBudgetMainType.SelectedItem).Value;
            DataSet ds = this.BMS.GetBudgetSubType(budget_type_parentid);
            if (ds == null)
            {
                MessageBox.Show("请检查网络连接！");
                return;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    cbBudgetSubType.Items.Add(
                        new ComboBoxItem(dr["budget_type_name"].ToString(),
                        dr["budget_type_id"].ToString()));
                }
                if(cbBudgetSubType.Items.Count > 0)
                    cbBudgetSubType.SelectedIndex = 0;
            }
        }

        

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.Reset();
        }

        private void dgvBudgetItem_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            Regex numreg = new Regex("^\\d+$");
            int sum = 0, budget_item_pricesum;
            
            if(dgvBudgetItem.Columns[e.ColumnIndex].Name == "BudgetItemPrice" ||
                dgvBudgetItem.Columns[e.ColumnIndex].Name == "BudgetItemCount")
            {
                if(!numreg.IsMatch(dgvBudgetItem.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString()))
                    dgvBudgetItem.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null;
            }


            foreach (DataGridViewRow dgvr in dgvBudgetItem.Rows)
            {
                if (dgvr.Cells["BudgetItemPrice"].Value != null &&
                    dgvr.Cells["BudgetItemCount"].Value != null &&
                    dgvr.Cells["BudgetItemPrice"].Value.ToString() != "" &&
                    dgvr.Cells["BudgetItemCount"].Value.ToString() != "")
                {
                    budget_item_pricesum = Convert.ToInt32(dgvr.Cells["BudgetItemPrice"].Value) *
                        Convert.ToInt32(dgvr.Cells["BudgetItemCount"].Value);
                    dgvr.Cells["BudgetItemPriceSum"].Value = budget_item_pricesum.ToString();
                    sum += budget_item_pricesum;
                }
            }
            this.tbBudgetPrice.Text = sum.ToString();

        }

        private void dgvBudgetItem_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
           
        }

        private void dgvBudgetItem_CellEnter(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}